package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseUserAddUserDto;
import com.kaili.basic.pojo.dto.DeviceDTO;
import com.kaili.basic.pojo.vo.DeviceVO;
import com.kaili.basic.service.IDeviceService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.exception.RestException;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;


/**
*  控制器
* @author:zhouyijian
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "设备登记", description = "设备登记相关API")
public class DeviceController {
    @Autowired
	IDeviceService deviceService;

	@GetMapping(value = "/device")
	@ApiOperation("设备登记分页查询")
	public Page<DeviceVO> queryPageList(@ModelAttribute DeviceDTO dto) {
		Page<DeviceVO> result =deviceService.queryPageList(dto);
		return result;
	}

	@GetMapping(value = "/device/modelList/{typeId}")
	@ApiOperation("设备型号查询")
	public List<DeviceVO> queryDeviceModelList(@ApiParam("typeId") @PathVariable("typeId")Integer typeId) {
		List<DeviceVO> result =deviceService.queryModelByTypeId(typeId);
		return result;
	}

    @PostMapping(value = "/device/add")
    @ApiOperation("设备登记/注册")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.ADD, key = "imei")
	public RestStatus addDevice(@RequestBody  @Validated DeviceDTO dto) {
		return deviceService.doCreateDevice(dto);
    }



    @PutMapping(value = "/device/edit")
    @ApiOperation("设备编辑")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.UPDATE, key = "imei")
	public RestStatus update(@RequestBody @Validated DeviceDTO dto) {
		return deviceService.updateDeviceByImei(dto);

    }

	@PutMapping(value = "/device/switchDeviceStatus/{imei}")
	@ApiOperation("设备启用/停用")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.UPDATE, key = "imei")
	public RestStatus switchFacilityStatus(@ApiParam("imei") @PathVariable("imei") String imei) {
		return deviceService.switchDeviceStatus(imei);

	}



    @DeleteMapping(value = "/device/{id}")
    @ApiOperation("删除")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.DELETE)
	public RestStatus del(@ApiParam("id") @PathVariable("id") Long id) {
		return deviceService.deleteByImei(id);
    }

	@PostMapping(value = "/device/import")
	@ApiOperation("设备登记信息导入")
	public RestStatus importExcel(@RequestParam("file") MultipartFile file) throws Exception {
		List<DeviceDTO> list = ExcelUtil.importExcel(file, 1, 2, DeviceDTO.class);

		for(DeviceDTO dto : list){
			deviceService.doCreateDevice(dto);
		}
		return CommonStatus.OK;
	}

	@GetMapping(value = "/device/export")
	@ApiOperation("设备登记信息导出")
	public void export(HttpServletResponse response, @ModelAttribute DeviceDTO dto) throws RestException {
		List<DeviceVO> list = deviceService.queryListByCondition(dto);
		ExcelUtil.exportExcel(list,"","设备登记信息",DeviceVO.class, UUID.randomUUID().toString()+".xls",response);
	}
}
